﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;

namespace QL_BenXe
{
    public partial class frm_QLCapNhatVe : Form
    {
        public frm_QLCapNhatVe()
        {
            InitializeComponent();
        }

        private void frm_UpdateVe_Load(object sender, EventArgs e)
        {
            cmb_TuyenDuong.DisplayMember = "TenTuyenDuong";
            cmb_TuyenDuong.ValueMember = "MaTuyenDuong";
            cmb_TuyenDuong.DataSource = TuyenDuongBUS.Select_All();

            dtg_Ve.AutoGenerateColumns = false;
            List<Ve> list = VeBUS.Select_Ve_DatTruoc();
            dtg_Ve.DataSource = list;
        }

        private void cmb_Xe_SelectedIndexChanged(object sender, EventArgs e)
        {
            cmb_GioXuatBen.DisplayMember = "GioXuatBen";
            cmb_GioXuatBen.DataSource = LichChayBUS.Select_GioXuatBen_ThemVe(cmb_TuyenDuong.SelectedValue.ToString(), cmb_Xe.SelectedValue.ToString());

            String GiaVe = GiaVeBUS.Select_GiaTien(cmb_Xe.SelectedValue.ToString(), cmb_TuyenDuong.SelectedValue.ToString());
            String tmp = "";
            while (GiaVe.Length > 3)
            {
                tmp = "." + GiaVe.Substring(GiaVe.Length - 3) + tmp;
                GiaVe = GiaVe.Substring(0, GiaVe.Length - 3);
            }
            GiaVe = GiaVe + tmp;
            lbl_TienVeMoi.Text = GiaVe;
        }

        private void cmb_TuyenDuong_SelectedIndexChanged(object sender, EventArgs e)
        {
            cmb_Xe.DisplayMember = "BienSo";
            cmb_Xe.ValueMember = "MaXe";
            cmb_Xe.DataSource = XeBUS.Select_Xe_by_MaTuyenDuong(cmb_TuyenDuong.SelectedValue.ToString(),dateTimePicker1.Value.ToShortDateString());
        }

        String Ngay = "";
        private void dtg_Ve_SelectionChanged(object sender, EventArgs e)
        {
            lbl_TenKhachHang.Text= dtg_Ve.CurrentRow.Cells[0].Value.ToString();
            lbl_TenKhachHang.Tag = dtg_Ve.CurrentRow.Cells[7].Value.ToString();
            lbl_KhachHangMoi.Text = dtg_Ve.CurrentRow.Cells[0].Value.ToString();
            lbl_KhachHangMoi.Tag = dtg_Ve.CurrentRow.Cells[7].Value.ToString();
            lbl_Xe.Text = dtg_Ve.CurrentRow.Cells[1].Value.ToString();
            lbl_Xe.Tag = dtg_Ve.CurrentRow.Cells[8].Value.ToString();
            lbl_Ghe.Text = dtg_Ve.CurrentRow.Cells[2].Value.ToString();
            lbl_Ghe.Tag = dtg_Ve.CurrentRow.Cells[9].Value.ToString();
            lbl_TuyenDuong.Text = dtg_Ve.CurrentRow.Cells[3].Value.ToString();
            lbl_TuyenDuong.Tag = dtg_Ve.CurrentRow.Cells[10].Value.ToString();
            Ngay = dtg_Ve.CurrentRow.Cells[4].Value.ToString();
            lbl_Ngay.Text = Ngay.Split(' ')[0];
            String GiaVe = dtg_Ve.CurrentRow.Cells[6].Value.ToString();
            lbl_GioXuatBen.Text = dtg_Ve.CurrentRow.Cells[11].Value.ToString();
            String tmp = "";
            while (GiaVe.Length > 3)
            {
                tmp = "." + GiaVe.Substring(GiaVe.Length - 3) + tmp;
                GiaVe = GiaVe.Substring(0, GiaVe.Length - 3);
            }
            GiaVe = GiaVe + tmp;
            lbl_TienVeCu.Text = GiaVe;
            //MessageBox.Show(dtg_Ve.CurrentRow.Cells[4].Value.ToString());
        }

        private void btn_ThanhToan_Click(object sender, EventArgs e)
        {
            try
            {
                bool b = VeBUS.ThanhToan(lbl_TenKhachHang.Tag.ToString(), lbl_Xe.Tag.ToString(), lbl_Ghe.Tag.ToString(), lbl_TuyenDuong.Tag.ToString(), Ngay.Split(' ')[0]);
                if (b)
                    MessageBox.Show("Thanh toán thành công !!!");
                dtg_Ve.DataSource = VeBUS.Select_Ve_DatTruoc();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btn_CapNhat_Click(object sender, EventArgs e)
        {
            //MessageBox.Show(lbl_GioXuatBen.Text);
            try
            {
                Ve ve_cu = new Ve();
                ve_cu.MaKhachHang = lbl_TenKhachHang.Tag.ToString();
                ve_cu.MaXe = lbl_Xe.Tag.ToString();
                ve_cu.MaGhe = lbl_Ghe.Tag.ToString();
                ve_cu.MaTuyenDuong = lbl_TuyenDuong.Tag.ToString();
                ve_cu.Ngay = Ngay.Split(' ')[0];
                ve_cu.GioXuatBen = lbl_GioXuatBen.Text;

                Ve ve_moi = new Ve();
                ve_moi.MaXe = cmb_Xe.SelectedValue.ToString();
                ve_moi.MaGhe = cmb_Ghe.SelectedValue.ToString();
                ve_moi.MaTuyenDuong = cmb_TuyenDuong.SelectedValue.ToString();
                ve_moi.GioXuatBen = cmb_GioXuatBen.Text;
                ve_moi.Ngay = dateTimePicker1.Value.ToShortDateString();

                bool b = VeBUS.Update(ve_cu, ve_moi);
                if (b)
                {
                    MessageBox.Show("Cập nhật thành công!!!");
                    dtg_Ve.DataSource = VeBUS.Select_Ve_DatTruoc();

                    GheBUS.Update_TinhTrang(ve_cu.MaGhe, "False");
                    GheBUS.Update_TinhTrang(ve_moi.MaGhe, "True");
                    XeBUS.CapNhatTinhTrangXeTheoSoGheConTrong(ve_moi.MaXe);

                    cmb_GioXuatBen.DisplayMember = "GioXuatBen";
                    cmb_GioXuatBen.DataSource = LichChayBUS.Select_GioXuatBen_ThemVe(cmb_TuyenDuong.SelectedValue.ToString(), cmb_Xe.SelectedValue.ToString());
                    
                    cmb_Xe.DisplayMember = "BienSo";
                    cmb_Xe.ValueMember = "MaXe";
                    cmb_Xe.DataSource = XeBUS.Select_Xe_by_MaTuyenDuong(cmb_TuyenDuong.SelectedValue.ToString(),dateTimePicker1.Value.ToShortDateString());
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void btn_Xoa_Click(object sender, EventArgs e)
        {
            try
            {
                bool b = VeBUS.Delete(lbl_TenKhachHang.Tag.ToString(), lbl_Xe.Tag.ToString(), lbl_Ghe.Tag.ToString(), lbl_TuyenDuong.Tag.ToString(), Ngay.Split(' ')[0]);
                if (b)
                {
                    MessageBox.Show("Xóa thành công !!!");

                    Ve ve = new Ve();
                    ve.MaXe = cmb_Xe.SelectedValue.ToString();
                    ve.MaTuyenDuong = cmb_TuyenDuong.SelectedValue.ToString();
                    ve.GioXuatBen = cmb_GioXuatBen.Text;
                    cmb_Ghe.DisplayMember = "TenGhe";
                    cmb_Ghe.ValueMember = "MaGhe";
                    cmb_Ghe.DataSource = GheBUS.Select_GheTrong(ve);
                    dtg_Ve.DataSource = VeBUS.Select_Ve_DatTruoc();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            cmb_TuyenDuong.DisplayMember = "TenTuyenDuong";
            cmb_TuyenDuong.ValueMember = "MaTuyenDuong";
            cmb_TuyenDuong.DataSource = LichChayBUS.Select_TuyenDuong_by_Ngay(dateTimePicker1.Value.ToShortDateString());
        }

        private void cmb_GioXuatBen_SelectedIndexChanged(object sender, EventArgs e)
        {
            Ve ve = new Ve();
            ve.MaXe = cmb_Xe.SelectedValue.ToString();
            ve.MaTuyenDuong = cmb_TuyenDuong.SelectedValue.ToString();
            ve.GioXuatBen = cmb_GioXuatBen.Text;
            cmb_Ghe.DisplayMember = "TenGhe";
            cmb_Ghe.ValueMember = "MaGhe";
            cmb_Ghe.DataSource = GheBUS.Select_GheTrong(ve);
        }
    }
}
